import _ from 'lodash'
import fetch from "node-fetch"
import puppeteer from '../../lib/puppeteer/puppeteer.js'

const regex = /^(#|\/)?姓名测算(.*)$/
export class example extends plugin {
    constructor() {
        super({
            name: '姓名测算',
            dsc: 'example',
            event: 'message',
            priority: 1,
            rule: [
                {
                    reg: regex,
                    fnc: 'Name_measurement'
                }
            ]
        })
    }
    async Name_measurement(e) {
        const Chinese_Regex = /^[\u4e00-\u9fa5]+$/;
        const Extract_name = e.msg.match(regex)
        const name = Extract_name[2]
        if (name.length > 3) { return e.reply('仅支持中国姓名，且名字最多三字') } else if (name.length === 0) { return e.reply('请在指令后面添加姓名发送') }
        if (!Chinese_Regex.test(name)) { return e.reply('请输入中文！也可能是该名字包含了非常见中文') }
        let xing, ming = ''
        if (name.length === 3) {
            xing = name[0]
            ming = name[1] + name[2]
        } else if (name.length === 2) {
            xing = name[0]
            ming = name[1]
        }
        const Fetch_Url_Data = await fetch(`https://oiapi.net/API/NameMea/?xing=${xing}&ming=${ming}`)
        const Parsing_Data = await Fetch_Url_Data.json();
        const Data_Info = Parsing_Data['data']
        const tian = Data_Info['tian']
        const ren = Data_Info['ren']
        const di = Data_Info['di']
        const wai = Data_Info['wai']
        const zong = Data_Info['zong']
        const destiny = Data_Info['destiny']

        const data = {
            tplFile: `data/Name measurement.html`,
            name: Data_Info['name'],
            数理: destiny['数理'],
            标题: destiny['标题'],
            基业: destiny['基业'],
            家庭: destiny['家庭'],
            健康: destiny['健康'],
            含义: destiny['含义'],
            Tian_Fen: tian['fen'],
            Tian_Shu: tian['five']['shu'],
            Tian_Wu: tian['five']['wu'],
            Tian_Ke: tian['five']['ke'],
            Tian_Sheng: tian['five']['sheng'],
            ren_Fen: ren['fen'],
            ren_Shu: ren['five']['shu'],
            ren_Wu: ren['five']['wu'],
            ren_Ke: ren['five']['ke'],
            ren_Sheng: ren['five']['sheng'],
            di_Fen: di['fen'],
            di_Shu: di['five']['shu'],
            di_Wu: di['five']['wu'],
            di_Ke: di['five']['ke'],
            di_Sheng: di['five']['sheng'],
            wai_Fen: wai['fen'],
            wai_Shu: wai['five']['shu'],
            wai_Wu: wai['five']['wu'],
            wai_Ke: wai['five']['ke'],
            wai_Sheng: wai['five']['sheng'],
            zong_Fen: zong['fen'],
            zong_Shu: zong['five']['shu'],
            zong_Wu: zong['five']['wu'],
            zong_Ke: zong['five']['ke'],
            zong_Sheng: zong['five']['sheng'],
        }
        const img = await puppeteer.screenshot('Name measurement', data)
        if (img) await this.e.reply(_.concat(img))
    }
}